//
//  IFIndexTitleView.m
//  IfengNews
//
//  Created by Ryan on 14-12-17.
//
//

#import "IFIndexTitleView.h"
#import <CoreText/CoreText.h>

@implementation IFIndexTitleView

- (instancetype)init{
    self=[super init];
    if(self){
        self.backgroundColor=[UIColor clearColor];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if(self){
        self.backgroundColor=[UIColor clearColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect{
    
    if (!self.text || self.text.length <= 0 || _attributeString==nil) {
        return;
    }
    
    CTFramesetterRef framesetter=CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)_attributeString);
    
    // 开始绘制
    CGMutablePathRef path=CGPathCreateMutable();
    CGPathAddRect(path, NULL, self.bounds);
    
    CTFrameRef frame=CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
    
    CGContextRef context=UIGraphicsGetCurrentContext();
    
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    CTFrameDraw(frame, context);
    
    CFRelease(frame);
    CGPathRelease(path);
    CFRelease(framesetter);
}

- (void)setTextColor:(UIColor *)atextColor{
    
    _textColor=atextColor;
    CGColorRef color=atextColor.CGColor;
    [self.attributeString addAttribute:(id)kCTForegroundColorAttributeName
                                 value:(__bridge id)color range:NSMakeRange(0, self.text.length)];
    [self setNeedsDisplay];
    
    
}

- (void)setTextColor:(UIColor *)textColor range:(NSRange)range{
    if (range.location>=self.text.length || (range.location+range.length)>self.text.length)
        return;
    
    CGColorRef color=textColor.CGColor;
    [self.attributeString addAttribute:(id)kCTForegroundColorAttributeName
                                 value:(__bridge id)color range:range];
    [self setNeedsDisplay];
}

- (void)_portrayTitleAttribute:(NSMutableAttributedString *)attributeStr{
    
    [attributeStr beginEditing];
    // 字体
    CTFontRef font=CTFontCreateWithName((__bridge CFStringRef)self.font.fontName, self.font.pointSize, NULL);
    [attributeStr addAttribute:(id)kCTFontAttributeName value:(__bridge id)font range:NSMakeRange(0, self.text.length)];
    CFRelease(font);
    
    // 字体颜色
    
    if(self.textColor){
    CGColorRef color=self.textColor.CGColor;
    [attributeStr addAttribute:(id)kCTForegroundColorAttributeName value:(__bridge id)color range:NSMakeRange(0, self.text.length)];
    }
    // 不允许缩字距
    //    // 字间距
    //    long number = -1.f;
    //    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
    //    [attributeStr addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0, self.text.length)];
    
    // 段落样式
    //对齐方式
    CTParagraphStyleSetting alignmentStyle;
    CTTextAlignment alignment = kCTTextAlignmentLeft;
    alignmentStyle.spec = kCTParagraphStyleSpecifierAlignment;//kCTParagraphStyleSpecifierLineSpacingAdjustment
    alignmentStyle.valueSize=sizeof(alignment);
    alignmentStyle.value=&alignment;
    //换行模式
    CTParagraphStyleSetting lineBreakMode;
    CTLineBreakMode lineBreak = kCTLineBreakByCharWrapping;//kCTLineBreakByWordWrapping;
    lineBreakMode.spec = kCTParagraphStyleSpecifierLineBreakMode;
    lineBreakMode.value = &lineBreak;
    lineBreakMode.valueSize = sizeof(CTLineBreakMode);
    //行距
    CTParagraphStyleSetting lineSpaceSetting;
    CGFloat _linespace = 1;
    lineSpaceSetting.spec = kCTParagraphStyleSpecifierLineSpacing;
    lineSpaceSetting.value = &_linespace;
    lineSpaceSetting.valueSize = sizeof(float);
    
    //
    
    
    //组合设置
    CTParagraphStyleSetting settings[] = {
        alignmentStyle,
        lineBreakMode,
        lineSpaceSetting,
    };
    //通过设置项产生段落样式对象
    CTParagraphStyleRef style = CTParagraphStyleCreate(settings, 3);
    
    [attributeStr addAttribute:(id)kCTParagraphStyleAttributeName value:(__bridge id)style range:NSMakeRange(0, self.text.length)];
    CFRelease(style);
    
    [attributeStr endEditing];
}

- (void)setText:(NSString *)text{
    if(_isStrNULL(text))return;
    if(![_text isEqualToString:text]){
        _text=text;
        self.attributeString = [[NSMutableAttributedString alloc] initWithString:self.text];
        [self _portrayTitleAttribute:self.attributeString];
        [self setNeedsDisplay];
    }
}

@end
